package com.xuan.yupao.controller;


import com.xuan.yupao.common.ErrorCode;
import com.xuan.yupao.common.Result;
import com.xuan.yupao.exception.BusinessException;
import com.xuan.yupao.model.domain.User;
import com.xuan.yupao.model.domain.request.MessagePrivateRequest;
import com.xuan.yupao.model.domain.request.MessageTeamRequest;
import com.xuan.yupao.model.domain.vo.MessageVo;
import com.xuan.yupao.model.domain.vo.PrivateMessageVo;
import com.xuan.yupao.service.MessageService;
import com.xuan.yupao.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private UserService userService;

    @Resource
    private MessageService messageService;

    @GetMapping("/private/total")
    public Result<Long> getNoRereadMessageNum(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Long num = messageService.getNoRereadMessageNum(loginUser.getId());
        return Result.success(num);
    }


    @GetMapping("/private/list")
    public Result<List<PrivateMessageVo>> getPrivateChatList(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        List<PrivateMessageVo> privateMessageVos = messageService.getPrivateChatList(loginUser.getId());
        return Result.success(privateMessageVos);
    }


    @PostMapping("/private/chat")
    public Result<List<MessageVo>> getPrivateChatHistory(@RequestBody MessagePrivateRequest messagePrivateRequest, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        log.info("[getPrivateChatHistory] toId=" + messagePrivateRequest);

        List<MessageVo> messageVoList =  messageService.getPrivateChatHistory(messagePrivateRequest.getToId(), loginUser.getId());
        return Result.success(messageVoList);
    }


    @PostMapping("/private/read")
    public Result<Boolean> confirmUnreadMessage(@RequestParam Long remoteId, HttpServletRequest request) {
        if (remoteId == null || remoteId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        log.info("[confirmUnreadMessage] remoteId=" + remoteId);

        User loginUser = userService.getLoginUser(request);
        boolean b = messageService.confirmUnreadMessage(remoteId, loginUser.getId());
        return Result.success(b);
    }


    @PostMapping("/teamChat")
    public Result<List<MessageVo>> getTeamChatHistory(@RequestBody MessageTeamRequest messageTeamRequest, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        log.info("[getTeamChatHistory] teamId=" + messageTeamRequest);

        List<MessageVo> messageVoList = messageService.getTeamChatHistory(messageTeamRequest.getTeamId(), loginUser.getId());
        return Result.success(messageVoList);
    }

    @GetMapping("/hallChat")
    public Result<List<MessageVo>> getHallChatHistory(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);

        List<MessageVo> messageVoList = messageService.getHallChatHistory(loginUser.getId());
        return Result.success(messageVoList);
    }
}
